package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.ExchangeFunctionEnum;
import com.ecube.protocol.exchange.station.ExchangeStationInfo;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;


/**
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/4 9:27
 */
public class SendExchangeStationInfo {

    public static void main(String[] args) {

        sendStationInfo("FAT", "100030001","126725", StationInfo.builder().stationId("126725").stationName("五寨三岔站").build());
        sendStationInfo("FAT", "100030001","400411", StationInfo.builder().stationId("400411").stationName("简称换电4111").build());
    }

    private static void sendStationInfo(String env, String productCode, String deviceCode, StationInfo info) {
        ExchangeStationInfo stationInfo = new ExchangeStationInfo();
        ExchangeFunctionEnum function = (ExchangeFunctionEnum) EnumUtils.getFunction(stationInfo.getClass());
        if(function == null){
            return;
        }
        stationInfo.setName(info.getStationName());
        stationInfo.setStationID(info.getStationId());

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V3.1.1");
        reportStandard.setHeader(reportHand);

        reportStandard.setDataBody(stationInfo);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    @Data
    @Builder
    private static class StationInfo {
        /**
         * 站外部编号
         */
        private String stationId;

        /**
         * 站名称
         */
        private String stationName;
    }
}
